const client = require('scp2');
const { prompt } = require('inquirer');
const ora = require('ora');
const chalk = require('chalk');
const { projectList, envList } = require('./enum.js');
const jsonConfig = require('./projectConfig.json');

async function selectConfig() {
  const questions = [
    {
      type: 'list',
      name: 'project',
      message: '请选择项目',
      choices: projectList,
      default: projectList[0].value
    },
    {
      type: 'list',
      name: 'env',
      message: '请选择环境',
      choices: envList,
      default: envList[0].value
    }
  ]
  const { project, env } = await prompt(questions)
  return {
    src: jsonConfig[project].src,
    ...jsonConfig[project]['envConfig'][env]
  }
}

async function uploadDistFile() {
  const { src, ...config } = await selectConfig()
  let spinner = ora('打包文件上传中...')
  console.log(chalk.blue('--------------上传基本信息--------------------'));
  console.log(chalk.blue(`本地文件路径: ${src}`));
  console.log(chalk.blue(`服务器地址: ${config.host}`))
  console.log(chalk.blue(`服务器路径: ${config.path}`))
  console.log(chalk.blue('----------------------------------'));
  spinner.start()
  client.scp(src, config, function (err) {
    spinner.stop()
    if (!err) {
      spinner = ora('打包文件上传成功')
      spinner.succeed()
      return
    }
    spinner = ora('上传失败')
    spinner.fail()
    console.log(err)
  })
}

uploadDistFile()
